 
package servlets.login;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import AO.Login;
/**
 * @author Administrator
 */
@WebServlet(name = "loginservlet", urlPatterns = {"/loginservlet"})
public class loginservlet extends HttpServlet {

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //PrintWriter out = response.getWriter();
        String userid=request.getParameter("userid");
        String password=request.getParameter("password");
        
        
       //=========================================================
       // ==============check if user exists or not===============
       //=========================================================
        
        if(helper.Login.isExists(userid, password))
            {
                
                request.getSession().invalidate();
            //helper.Login.getType(userid, password);
            Login login =helper.Login.getLoginBean(userid, password);
            request.getSession().setAttribute("logindata", login);
            //System.out.println("the login user id is  "+login.getUserid());
            if(login.getType().equals("student"))
            {
               request.getSession().setAttribute("userdata", helper.Login.getStudentBean(userid));
              if(login.getIsEnabled()==true)
               response.sendRedirect("./student/");
              else
                  response.sendRedirect("./student/registration/showprofile.jsp");
            }
            else if(login.getType().equals("faculty"))
            {
            request.getSession(true).setAttribute("userdata", helper.Login.getFacultyBean(userid));
            if(login.getIsEnabled()==true)
            response.sendRedirect("./faculty/");
            else
                  response.sendRedirect("./faculty/registration/showprofile.jsp");
            }
            
            else if(login.getType().equals("admin"))
            {
                request.getSession(true).setAttribute("adminbean", helper.Login.getAdminBean(userid,password));
                response.sendRedirect("./administrator/");
            }
            }
       
            
    }
    
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
